<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
 <head>
  <meta http-equiv="content-type" content="text/html; charset=UTF-8">
  <title>使用远程文件</title>

 </head>
 <body><div class="manualnavbar" style="text-align: center;">
 <div class="prev" style="text-align: left; float: left;"><a href="features.file-upload.put-method.html">对 PUT 方法的支持</a></div>
 <div class="next" style="text-align: right; float: right;"><a href="features.connection-handling.html">连接处理</a></div>
 <div class="up"><a href="features.html">特点</a></div>
 <div class="home"><a href="index.html">PHP Manual</a></div>
</div><hr /><div id="features.remote-files" class="chapter">
 <h1>使用远程文件</h1>


 <p class="para">
  只要在 <var class="filename">php.ini</var> 文件中激活了 <strong class="option unknown">allow_url_fopen</strong>

  选项，就可以在大多数需要用文件名作为参数的函数中使用
  <acronym title="Hypertext Transfer Protocol">HTTP</acronym> 和 <acronym title="File Transfer Protocol">FTP</acronym> 的 URL
  来代替文件名。同时，也可以在
  <span class="function"><a href="function.include.html" class="function">include</a></span>、<span class="function"><a href="function.include-once.html" class="function">include_once</a></span>、<span class="function"><a href="function.require.html" class="function">require</a></span> 及
  <span class="function"><a href="function.require-once.html" class="function">require_once</a></span> 语句中使用 URL。PHP
  所支持协议的更多信息参见<a href="wrappers.html" class="xref">支持的协议和封装协议</a>。
 </p>

 <blockquote class="note"><p><strong class="note">Note</strong>: 
  <p class="para">
   要在 PHP 4.0.3 及其更早的版本中使用 URL 封装协议，需要在编译时用
  <strong class="option unknown">--enable-url-fopen-wrapper</strong>
 参数来配置 PHP。
  </p>
 </p></blockquote>

 <blockquote class="note"><p><strong class="note">Note</strong>: 
  <p class="para">
   Windows 版本的 PHP 4.3
   版之前不支持以下函数的远程访问：<span class="function"><a href="function.include.html" class="function">include</a></span>，<span class="function"><a href="function.include-once.html" class="function">include_once</a></span>，<span class="function"><a href="function.require.html" class="function">require</a></span>，<span class="function"><a href="function.require-once.html" class="function">require_once</a></span>
   以及<a href="ref.image.html" class="xref">GD and Image 函数</a>中的 imagecreatefromXXX 函数。
  </p>
 </p></blockquote>

 <p class="para">
  例如，可以用以下范例来打开远程 web
  服务器上的文件，解析需要的输出数据，然后将这些数据用在数据库的检索中，或者简单地以和自己网站其它页面相同的风格输出其内容。
 </p>

 <p class="para">
  <div class="example" id="example-355">
   <p><strong>Example #1 获取远程文件的标题</strong></p>
   <div class="example-contents">
<div class="phpcode"><code><span style="color: #000000">
<span style="color: #0000BB">&lt;?php<br />$file&nbsp;</span><span style="color: #007700">=&nbsp;</span><span style="color: #0000BB">fopen&nbsp;</span><span style="color: #007700">(</span><span style="color: #DD0000">"http://www.example.com/"</span><span style="color: #007700">,&nbsp;</span><span style="color: #DD0000">"r"</span><span style="color: #007700">);<br />if&nbsp;(!</span><span style="color: #0000BB">$file</span><span style="color: #007700">)&nbsp;{<br />&nbsp;&nbsp;&nbsp;&nbsp;echo&nbsp;</span><span style="color: #DD0000">"&lt;p&gt;Unable&nbsp;to&nbsp;open&nbsp;remote&nbsp;file.\n"</span><span style="color: #007700">;<br />&nbsp;&nbsp;&nbsp;&nbsp;exit;<br />}<br />while&nbsp;(!</span><span style="color: #0000BB">feof&nbsp;</span><span style="color: #007700">(</span><span style="color: #0000BB">$file</span><span style="color: #007700">))&nbsp;{<br />&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #0000BB">$line&nbsp;</span><span style="color: #007700">=&nbsp;</span><span style="color: #0000BB">fgets&nbsp;</span><span style="color: #007700">(</span><span style="color: #0000BB">$file</span><span style="color: #007700">,&nbsp;</span><span style="color: #0000BB">1024</span><span style="color: #007700">);<br />&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #FF8000">/*&nbsp;This&nbsp;only&nbsp;works&nbsp;if&nbsp;the&nbsp;title&nbsp;and&nbsp;its&nbsp;tags&nbsp;are&nbsp;on&nbsp;one&nbsp;line&nbsp;*/<br />&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #007700">if&nbsp;(</span><span style="color: #0000BB">eregi&nbsp;</span><span style="color: #007700">(</span><span style="color: #DD0000">"&lt;title&gt;(.*)&lt;/title&gt;"</span><span style="color: #007700">,&nbsp;</span><span style="color: #0000BB">$line</span><span style="color: #007700">,&nbsp;</span><span style="color: #0000BB">$out</span><span style="color: #007700">))&nbsp;{<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #0000BB">$title&nbsp;</span><span style="color: #007700">=&nbsp;</span><span style="color: #0000BB">$out</span><span style="color: #007700">[</span><span style="color: #0000BB">1</span><span style="color: #007700">];<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;break;<br />&nbsp;&nbsp;&nbsp;&nbsp;}<br />}<br /></span><span style="color: #0000BB">fclose</span><span style="color: #007700">(</span><span style="color: #0000BB">$file</span><span style="color: #007700">);<br /></span><span style="color: #0000BB">?&gt;</span>
</span>
</code></div>
   </div>

  </div>
 </p>

 <p class="para">
  如果有合法的访问权限，以一个用户的身份和某 FTP
  服务器建立了链接，还可以向该 FTP
  服务器端的文件进行写操作。仅能用该方法来创建新的文件；如果尝试覆盖已经存在的文件，<span class="function"><a href="function.fopen.html" class="function">fopen()</a></span>
  函数的调用将会失败。
 </p>

 <p class="para">
  要以“anonymous”以外的用户名连接服务器，需要指明用户名（可能还有密码），例如“ftp://user:password@ftp.example.com/path/to/file”（也可以在通过需要
  Basic 认证的 HTTP 协议访问远程文件时使用相同的语法）。
 </p>

 <p class="para">
  <div class="example" id="example-356">
   <p><strong>Example #2 将数据保存到远程服务器</strong></p>
   <div class="example-contents">
<div class="phpcode"><code><span style="color: #000000">
<span style="color: #0000BB">&lt;?php<br />$file&nbsp;</span><span style="color: #007700">=&nbsp;</span><span style="color: #0000BB">fopen&nbsp;</span><span style="color: #007700">(</span><span style="color: #DD0000">"ftp://ftp.example.com/incoming/outputfile"</span><span style="color: #007700">,&nbsp;</span><span style="color: #DD0000">"w"</span><span style="color: #007700">);<br />if&nbsp;(!</span><span style="color: #0000BB">$file</span><span style="color: #007700">)&nbsp;{<br />&nbsp;&nbsp;&nbsp;&nbsp;echo&nbsp;</span><span style="color: #DD0000">"&lt;p&gt;Unable&nbsp;to&nbsp;open&nbsp;remote&nbsp;file&nbsp;for&nbsp;writing.\n"</span><span style="color: #007700">;<br />&nbsp;&nbsp;&nbsp;&nbsp;exit;<br />}<br /></span><span style="color: #FF8000">/*&nbsp;Write&nbsp;the&nbsp;data&nbsp;here.&nbsp;*/<br /></span><span style="color: #0000BB">fwrite&nbsp;</span><span style="color: #007700">(</span><span style="color: #0000BB">$file</span><span style="color: #007700">,&nbsp;</span><span style="color: #0000BB">$_SERVER</span><span style="color: #007700">[</span><span style="color: #DD0000">'HTTP_USER_AGENT'</span><span style="color: #007700">]&nbsp;.&nbsp;</span><span style="color: #DD0000">"\n"</span><span style="color: #007700">);<br /></span><span style="color: #0000BB">fclose&nbsp;</span><span style="color: #007700">(</span><span style="color: #0000BB">$file</span><span style="color: #007700">);<br /></span><span style="color: #0000BB">?&gt;</span>
</span>
</code></div>
   </div>

  </div>
 </p>

 <blockquote class="note"><p><strong class="note">Note</strong>: 
  <p class="para">
   或许可以从以上范例中得到启发，用该技术来存储远程日志文件。但是正如以上提到的，在用
    <span class="function"><a href="function.fopen.html" class="function">fopen()</a></span> 方式打开的 URL
    中，仅能对新文件进行写操作。如果远程文件已经存在则
    <span class="function"><a href="function.fopen.html" class="function">fopen()</a></span> 函数的操作将会失败。要做类似于分布式日志的事，可以参考
    <span class="function"><a href="function.syslog.html" class="function">syslog()</a></span> 函数。
  </p>
 </p></blockquote>

</div>
<hr /><div class="manualnavbar" style="text-align: center;">
 <div class="prev" style="text-align: left; float: left;"><a href="features.file-upload.put-method.html">对 PUT 方法的支持</a></div>
 <div class="next" style="text-align: right; float: right;"><a href="features.connection-handling.html">连接处理</a></div>
 <div class="up"><a href="features.html">特点</a></div>
 <div class="home"><a href="index.html">PHP Manual</a></div>
</div></body></html>
